package cloud.agileframework.common.util.ip;

import cloud.agileframework.common.util.clazz.TypeReference;
import cloud.agileframework.common.util.object.ObjectUtil;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.reader.ObjectReader;

import java.lang.reflect.Type;

public class IPReader implements ObjectReader<Long> {
    @Override
    public Long readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        Object value = jsonReader.readAny();
        if (value instanceof String) {
            return ObjectUtil.to(IPv4Util.ipToLong(value.toString()), new TypeReference<>(fieldType));
        }
        return null;
    }
}
